//
//  EditPanelTableViewCell.swift
//  ezyform
//
//  Created by Moennig Q on 8/9/2022.
//  Copyright © 2022 Client.Solution Pty Ltd. All rights reserved.
//
import UIKit
import RxSwift
import RxGesture
import RxCocoa
class EditPanelTableViewCell: TableViewCell {
    var selectModelClosure: (()->Void)?
    var deleteRowClosure: (()->Void)?
    lazy var brandTextfield: DIYTextField = {
        let view = DIYTextField()
        view.clearButtonMode = .never
        view.disabled(textHexStr: "#292D3266", backHexStr: "#E3E3E3FF")
        return view
    }()
    lazy var modelTextfield: TextFieldWithRequiredView = {
        let view = TextFieldWithRequiredView()
        view.textField.isEnabled = false
        view.textField.clearButtonMode = .never
        view.textField.showRightImageView("installation_list_search_down_arrow")
        return view
     }()
    lazy var wattsTextfield: TextFieldWithRequiredView = {
        return TextFieldWithRequiredView(.positiveNumber)
     }()
    lazy var countTextfield: TextFieldWithRequiredView = {
        return TextFieldWithRequiredView(.positiveNumber3)
     }()
    lazy var outputTextfield: DIYTextField = {
        let view = DIYTextField()
        view.clearButtonMode = .never
        view.disabled(textHexStr: "#292D3266", backHexStr: "#E3E3E3FF")
        return view
     }()
    lazy var deleteBtn: UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "PVDDetail_systeminfo_del"), for: UIControl.State.normal)
        return btn
     }()
    
    override func setUpviews(){
        //backView
        let backView = UIView()
        backView.drawBorder(backHexStr: "#F1F4F8FF", borderHexStr: "#34609666")
        self.contentView.addSubview(backView)
        backView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview().offset(-Constant.LeftRightMargin)
        }
        //brandTitle
        let brandTitle = ContentLabel()
        brandTitle.text = "Brand"
        backView.addSubview(brandTitle)
        brandTitle.snp.makeConstraints { make in
            make.top.equalTo(Constant.TopBottomMargin)
            make.left.equalTo(Constant.LeftRightMargin)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //delete btn
        backView.addSubview(deleteBtn)
        deleteBtn.snp.makeConstraints { make in
            make.top.right.equalToSuperview()
            make.width.height.equalTo(40)
        }
        //brandTextfield
        backView.addSubview(brandTextfield)
        brandTextfield.snp.makeConstraints { make in
            make.top.equalTo(brandTitle.snp.bottom).offset(Constant.TitleTextMargin)
            make.left.equalTo(brandTitle)
            make.right.equalTo(-Constant.LeftRightMargin)
            make.height.equalTo(44)
        }
        //modelTitle
        let modelTitle = TitleLabelWithRequired(R.string.localizable.model())
        backView.addSubview(modelTitle)
        modelTitle.snp.makeConstraints { make in
            make.top.equalTo(brandTextfield.snp.bottom).offset(Constant.TopBottomMargin)
            make.left.equalTo(brandTitle)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //modelTextfield
        backView.addSubview(modelTextfield)
        modelTextfield.snp.makeConstraints { make in
            make.top.equalTo(modelTitle.snp.bottom).offset(Constant.TitleTextMargin)
            make.left.right.equalTo(brandTextfield)
            make.height.greaterThanOrEqualTo(0)
        }
        //wattsTitle
        let wattsTitle = TitleLabelWithRequired(R.string.localizable.wattsPerPanelW())
        backView.addSubview(wattsTitle)
        wattsTitle.snp.makeConstraints { make in
            make.top.equalTo(modelTextfield.snp.bottom).offset(Constant.TopBottomMargin)
            make.left.equalTo(brandTitle)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //wattsTextfield
        backView.addSubview(wattsTextfield)
        wattsTextfield.snp.makeConstraints { make in
            make.top.equalTo(wattsTitle.snp.bottom).offset(Constant.TitleTextMargin)
            make.left.right.equalTo(brandTextfield)
            make.height.equalTo(55)//greaterThanOrEqualTo(0),暂时处理this is requird不能正常显示的问题
        }
        //numberTitle
        let numberTitle = TitleLabelWithRequired(R.string.localizable.qty())
        backView.addSubview(numberTitle)
        numberTitle.snp.makeConstraints { make in
            make.top.equalTo(wattsTextfield.snp.bottom).offset(Constant.TopBottomMargin)
            make.left.equalTo(brandTitle)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //countTextfield
        backView.addSubview(countTextfield)
        countTextfield.snp.makeConstraints { make in
            make.top.equalTo(numberTitle.snp.bottom).offset(Constant.TitleTextMargin)
            make.left.equalTo(brandTextfield)
            make.height.equalTo(55)//greaterThanOrEqualTo(0),暂时处理this is requird不能正常显示的问题
            make.width.greaterThanOrEqualTo(0)
            make.bottom.equalTo(-Constant.TopBottomMargin)
        }
        //outputTextfield
        backView.addSubview(outputTextfield)
        outputTextfield.snp.makeConstraints { make in
            make.top.equalTo(countTextfield)
            make.left.equalTo(countTextfield.snp.right).offset(Constant.InternalMargin)
            make.right.equalTo(brandTextfield)
            make.width.equalTo(countTextfield)
            make.height.equalTo(DIYTextField.height)
        }
        //outputTitle
        let outputTitle = ContentLabel(text: R.string.localizable.outputKW())
        backView.addSubview(outputTitle)
        outputTitle.snp.makeConstraints { make in
            make.top.equalTo(numberTitle)
            make.left.equalTo(outputTextfield)
            make.width.height.greaterThanOrEqualTo(0)
        }
        deleteBtn.addTarget(self, action: #selector(deleteBtnClick), for: UIControl.Event.touchUpInside)
        //点击选择model
        self.modelTextfield.rx.tapGesture().when(.recognized).subscribe(onNext: {[weak self] _ in
            guard let strongSelf = self else {return}
            strongSelf.selectModelClosure?()
        }).disposed(by: self.disposeBag)
    }
    @objc func deleteBtnClick(){
        self.endEditing(true)
        self.deleteRowClosure?()
    }
    func setData(_ list: [InstallationDetailSystemDetailForPanelModel],row: Int){
        guard list.count > row else {
            return
        }
        let panelModel  = list[row]
        self.brandTextfield.text = panelModel.brand
        self.modelTextfield.textField.text = panelModel.model
        self.wattsTextfield.textField.text = (panelModel.watts_per_panel == 0 ? "" : "\(panelModel.watts_per_panel)")
        self.countTextfield.textField.text = (panelModel.number_panels == 0 ? "" : "\(panelModel.number_panels)")
        self.outputTextfield.text = panelModel.output
        //
        if list.count == 1{//只剩一行，不显示删除按钮
            self.deleteBtn.isHidden = true
        }else{
            self.deleteBtn.isHidden = false
        }
    }
}
